package com.bdzl.admin.controller.data;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import com.bdzl.admin.dao.data.VehModelDao;
import com.bdzl.admin.util.excel.StudentListener;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.vo.data.VehModelVO;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.bdzl.pub.entity.data.VehModelDO;
import com.bdzl.admin.service.data.VehModelService;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 车型表
 * 
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 13:57:43
 */
 
@Controller
@RequestMapping("/bdzl/vehModel")
public class VehModelController extends BaseController {
	@Autowired
	private VehModelService vehModelService;
	@Autowired
	private VehModelDao vehModelDao;
	
	@GetMapping()
	String VehModel(){
	    return "bdzl/vehModel/vehModel";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<VehModelDO> vehModelList = vehModelService.list(query);
		int total = vehModelService.count(query);
		PageUtils pageUtils = new PageUtils(vehModelList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "bdzl/vehModel/add";
	}

	@GetMapping("/edit/{modelId}")
	@ResponseBody
	R edit(@PathVariable("modelId") Integer modelId,Model model){
		VehModelDO vehModel = vehModelService.get(modelId);
		return R.ok(vehModel);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( @RequestBody @Validated VehModelDO vehModel){
		vehModel.setModelCreuser(super.getUserId());
		vehModel.setModelModuser(super.getUserId());
		if(vehModelService.save(vehModel)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( @RequestBody @Validated VehModelDO vehModel){
		vehModelService.update(vehModel);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@DeleteMapping( "/remove/{modelId}")
	@ResponseBody
	public R remove( @PathVariable("modelId") Integer modelId){
		if(vehModelService.remove(modelId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] modelIds){
		vehModelService.batchRemove(modelIds);
		return R.ok();
	}

	@ResponseBody
	@GetMapping("/vehAllList")
	public PageUtils vehAll(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<VehModelVO> vehModelList = vehModelService.vehAll(query);
		int total = vehModelService.vehAllcount(query);
		PageUtils pageUtils = new PageUtils(vehModelList, total);
		return pageUtils;
	}
	// 导出
	@GetMapping("download")
	public void download(HttpServletResponse response) throws IOException {
		//获取要导出的list对象
		List<VehModelVO> vehModelList = vehModelService.vehAll(null);
		// 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
		String fileName = URLEncoder.encode("测试", "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		//   Student.class 是按导出类  data()应为数据库查询数据，这里只是模拟
		EasyExcel.write(response.getOutputStream(), VehModelVO.class).sheet("模板").doWrite(vehModelList);
	}

	/**
	 * 读取 excel
	 * @return
	 */
	@PostMapping("upload")
	@ResponseBody
	public R upload(MultipartFile file) throws IOException {
		//写法一
		// sheet里面可以传参 根据sheet下标读取或者根据名字读取 不传默认读取第一个
		EasyExcel.read(file.getInputStream(), VehModelVO.class, new StudentListener(vehModelDao)).sheet().doRead();
		// 写法2：
        /*ExcelReader excelReader = EasyExcel.read(file.getInputStream(), Student.class, new StudentListener(studentDao)).build();
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        excelReader.read(readSheet);
        // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
        excelReader.finish();*/
		return R.ok();
	}

	
}
